package com.mmd.core.utils;

import java.util.UUID;

public class TokenUtils {
    /*小写*/
    public static String lowerCase = "lowerCase";
    /*大写*/
    public static String upperCase = "upperCase";

    /**
     * 创建token
     * @param quantity 长度
     * @return String
     */
    public static String createToken(Integer quantity)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        StringBuilder stringBuilder = new StringBuilder(uuid);
        stringBuilder.replace(0,1,aAndZ(upperCase));
        if (stringBuilder.toString().length() < quantity) {
            quantity = quantity - stringBuilder.toString().length();
            for (int i = 0; i < quantity; i++) {
                stringBuilder.append(aAndZ(upperCase));
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 创建大小写token
     * @param quantity 长度
     * @param type {@link TokenUtils#lowerCase}{@link TokenUtils#upperCase}
     * @return String
     */
    public static String createToken(Integer quantity, String type)
    {
        if(type.equals(lowerCase)){
            return createTokenLowerCase(quantity);
        }
        return createTokenUpperCase(quantity);
    }

    /**
     * 创建小写token
     * @param quantity 长度
     * @return String
     */
    public static String createTokenLowerCase(Integer quantity)
    {
        return createToken(quantity).toLowerCase();
    }

    /**
     * 创建大写token
     * @param quantity 长度
     * @return String
     */
    public static String createTokenUpperCase(Integer quantity)
    {
        return createToken(quantity).toUpperCase();
    }

    /**
     * A-Z
     * @param type {@link TokenUtils#lowerCase}{@link TokenUtils#upperCase}
     * @return String
     */
    public static String aAndZ(String type)
    {
        String[] str = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        Integer random = RandomUtils.random(0, 25);
        if (type.equals(lowerCase)) {
            return str[random].toLowerCase();
        }
        return str[random];
    }
}
